<template>
	<view>
		<view class="maskbox" v-if="showMail"></view>
		<view class="dialog" v-if="showMail">
			<view class="dtitle">
				<text>信息</text>
			</view>
			<view class="dcon">
				<view class="dtip">{{tip}}</view>
				<view class="xzk" v-if="xlksfxs">
					<combox-search label="收件人：" labelWidth="110rpx" emptyTips="无数据" :unikey="5"
						:candidates="candidates2" :isXzClear="true" placeholder="请输入邮箱地址" @getValue="getValue($event)"
						@input="jtValue($event)" :value="mail" :showMask="false"></combox-search>
					<button type="primary" size="mini" @click="addMail">添加</button>
				</view>
				<view class="zbMail">
					<view v-for="(item,index) in arrMail" :key="index" class="zbMailItem">
						<view class="yfsdMail">{{item}}</view>
						<icon type="clear" size="16" @click="cleanMail(item)" />
					</view>
				</view>
			</view>
			<view class="dbtn">
				<button type="primary" size="mini" @click="yjfs">确定</button>
				<button type="primary" size="mini" @click="closeMail">取消</button>
			</view>
		</view>
	</view>
</template>

<script>
	import comboxSearch from "@/components/cuihai-combox/cuihai-combox.vue"
	import {getNsrAndCzrByQO} from "@/utils/common-api.js"
	export default {
		name: 'yjfs',
		components: {
			comboxSearch,
		},
		data() {
			return {
				ylMailData:[],
				candidates2:[],
				mail: "",
				arrMail: [], //邮件数组
				yjfsData: "", //邮件发送数据
				xlksfxs:false
			};
		},
		props: {
			sbId:{
				type:String,
				default: ""
			},
			showMail: {
				type: Boolean,
				default: false
			},
			tip:{
				type:String,
				default: ""
			},
		},
		watch: {
			showMail:{
				async handler(newVal) {
						if (newVal) {
						  await this.kjfswszm(this.sbId)	
						}
					},
					immediate: true
				}
		},
		methods: {
			// 显示邮件发送完税证明弹框
			async kjfswszm(id) {
				this.xlksfxs=false
				// 获取出租人和承租人新增租赁时的邮件地址
				this.ylMailData=[]
				this.candidates2 = []
				this.arrMail=[]
				let res = await getNsrAndCzrByQO({
					sbId: id
					// sbId: "963cfdfcd64f45ec85b005ab3411c42d"
				})
				if (res.head.errorCode == "0") {
					res.body.map(item => {
						this.ylMailData.push({
							nsrmc: item.nsrmc,
							yxdz: item.yxdz
						})
						this.candidates2.push(item.nsrmc + "(" + item.yxdz + ")")
					})
				}
				this.xlksfxs=true
			},
			// 监听value值的改变
			jtValue(e) {
				this.mail = e
			},
			// 获取下来框选中邮件号码
			getValue(e) {
				// console.log(e)
				// 邮箱地址是否重复
				if (this.yjcfJy(e)) {
					return
				}
				this.arrMail.push(e)
			},
			// 输入添加邮件号码
			addMail() {
				// 邮箱地址本能为空
				if (!this.mail) {
					uni.showToast({
						title: '请输入邮箱地址',
						icon: 'error',
						duration: 1000
					});
					return
				}
				// 邮箱地址校验
				if (!this.$commonVaild.judgeYxdz(this.mail)) {
					uni.showToast({
						title: '邮箱地址错误',
						icon: 'error',
						duration: 1000
					});
					return
				}
				// 邮箱地址是否重复
				if (this.yjcfJy(this.mail)) {
					return
				}
				this.arrMail.push(this.mail)
				this.mail = ""
			},
			// 邮件重复校验
			yjcfJy(val) {
				// 邮箱地址是否重复
				let mailCf = false
				// console.log(this.arrMail)
				this.arrMail.map(item => {
					if (item == val) {
						mailCf = true
					}
				})
				if (mailCf) {
					uni.showToast({
						title: '邮箱地址已添加',
						icon: 'error',
						duration: 1000
					});
				}
				// console.log(mailCf)
				return mailCf
			},
			// 删除要发送的邮件
			cleanMail(val) {
				this.arrMail = this.arrMail.filter(item => {
					return val != item
				})
			},
			// 邮件发送
			async yjfs() {
				if (this.arrMail.length == 0) {
					uni.showToast({
						title: '邮箱不能为空',
						icon: 'error',
						duration: 1000
					});
					return
				}
				let val = this.yjfsData
				// 邮箱过滤
				let arrMail = this.arrMail.map(item => {
					this.ylMailData.map(ylItem => {
						let zhMail = ylItem.nsrmc + "(" + ylItem.yxdz + ")"
						if (item == zhMail) {
							item = ylItem.yxdz
						}
					})
					return item
				})
				this.$emit("yjfs",arrMail)
			},
			// 发送邮件信息弹框取消
			closeMail() {
				this.$emit("closeMail")
				this.mail = ""
				this.arrMail = []
			},
		}
	}
</script>

<style>
	::v-deep .uni-group .uni-group__content {
		padding: 0;
	}

	.uni-group__content {
		padding: 0;
	}


	.uni-margin-wrap {
		min-height: 700rpx;
	}

	.swiper {
		min-height: 680rpx;
	}

	::v-deep uni-swiper .uni-swiper-dots-horizontal {
		white-space: pre-wrap !important;
		width: 90%;
	}

	::v-deep uni-swiper .uni-swiper-dots-horizontal .uni-swiper-dot {
		margin-top: 10rpx;
	}

	.dialog {
		width: 95%;
		min-height: 750rpx;
		position: fixed;
		top: 30%;
		left: 50%;
		transform: translate(-50%, -30%);
		border: 2rpx solid black;
		background: #ffffff;
		z-index: 100;
	}

	.dialog .dtitle {
		width: 100%;
		padding: 10rpx;
		box-sizing: border-box;
		color: #ffffff;
		background: #3171d5;
	}

	.dialog .dtip {
		margin: 20rpx 0 10rpx 130rpx;
		font-weight: bold;
		font-size: 26rpx;
		text-align: left;
	}

	.xzk {
		padding: 10rpx;
		display: flex;
		align-items: center;
	}

	.xzk uni-button {}

	.xzk ::v-deep .uni-combox__label {
		text-align: right;
		font-weight: bold;
		font-size: 26rpx;
		color: black;
	}

	.xzk ::v-deep .combox_input_view {
		width: 400rpx;
	}

	.zbMail {
		padding: 0 20rpx;
		max-height: 340rpx;
		overflow: scroll
	}

	.yfsdMail {
		padding: 10rpx 0;
		word-break: break-word; //文本超出 自动换行
	}

	.zbMailItem {
		display: flex;
		align-items: center;
	}

	.zbMailItem ::v-deep .uni-icon-clear {
		margin-left: 10rpx;
	}

	.dialog .dbtn {
		width: 100%;
		text-align: center;
		position: absolute;
		bottom: 20rpx;
	}

	.dialog .dbtn uni-button {
		margin: 0 10rpx;
	}

	.maskbox {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		height: 100vh;
		z-index: 99;
		background-color: rgba(0, 0, 0, 0.4);
	}

	::v-deep .uni-pagination__btn {
		background: #00000063;
		opacit: 1;
	}

	::v-deep .uni-pagination__child-btn {
		color: #ffffff;
	}
</style>